/**
 *  by tangjk
 */

window.CardLove = window.CardLove || {};

/**
 * 单张Card对象
 */
CardLove.Card = function(){

	this.id = '';
	
    //card名字
    this.name = "";

    //类型（0：自己，1：对方）
    this.type = 0;
    //是否在得分区
    this.isInScoring = false;

    //card牌的图片URL地址
    this.url = "about:blank";

    //card牌图片的宽度
    this.imageWidth = 100;

    //card牌图片的高度
    this.imageHeight = 100;
    //img节点
    this.imgNode = new Image();
    
    this.areaIndex = 0;
};
/**
 * Card对象初始化方法
 * @param {String} id
 * @param {String} src
 */
CardLove.Card.prototype.init = function(id,src){
	this.id = id;
	this.imgNode.src = src;
	this.url = src;
	this.isInScoring = false;
}
/**
 * 把卡片
 * @param {} eleId
 */
CardLove.Card.prototype.drawImg = function(tag,ele){
	var imgNode = this.imgNode.cloneNode(true);
	imgNode.id = tag +'_card_'+this.id;
	var container =ele;
	container.innerHTML = '';
	container.appendChild(imgNode);
};
/**
 * 卡片缓存区
 * 
 * @type
 */
CardLove.Card._cache = {
	/**
	 * 卡牌缓存区
	 * @type 
	 */
	cards : {},
	/**
	 * 系统卡组
	 * @type 
	 */
	sysCards : {},
	/**
	 * 系统容器
	 */
	sysContainers : $('#js_card1 div'),
	/**
	 * 玩家卡组
	 * @type 
	 */
	playCards : {},
	playContainers : $('#js_card2 div'),
	status : {
		isMove : false
	},
	sysCurrentShowCard : '',
	queue : [],
	selectNode : ''
};
/**
 * 进行卡片初始化操作
 * 
 * 1.加载图片元素
 * 
 */
CardLove.Card.initCard = function(){
	//加载所有图片并且初始化card对象
	jQuery.each(CardLove.Config.imgList.src,function(index,src){
		var card = new CardLove.Card();
		card.init(CardLove.Config.imgList.cardId[index],src);
		CardLove.Card._cache.cards[card.id] = card;
	});

}; 
/**
 * 进行事件绑定
 * @param {Function} callback 回调函数
 */
CardLove.Card.bindEvent = function(callback){
	var nodeObj = CardLove.Config.imgList.node;
	$('#' + nodeObj[0].container + ',#'+nodeObj[1].container).delegate('div','click',callback);
};
/**
 * 得到用户的牌
 * 并且进行系统出牌顺序排序
 * @return {Object} 
 * 			sys : 系统的牌
 * 			player ： 玩家的牌
 */
CardLove.Card.getRoleCard = function(){
	//系统出牌顺序进行随机
	var i = 0;
	for(;i<CardLove.Config.card.randTime;i++){
		CardLove.Config.card.sysShowCardArray.sort(function(){return Math.random() > 0.5 ? 2 : -2});
	}
	
	var i = 0;
	for(;i<CardLove.Config.card.randTime;i++){
		CardLove.Config.card.roleCardList.sort(function(){return Math.random() > 0.5 ? 2 : -2});
	}
	var sysrole = CardLove.Card._cache.sysCards = {};
	var player = CardLove.Card._cache.playCards = {};
	
	jQuery.each(CardLove.Config.card.roleCardList,function(index,value){
		var card = CardLove.Card._cache.cards[value];
		if(index % 2 == 1) {
			sysrole[card.id] = card;
			card.type = 1;
		}else{ 
			player[card.id] = card;
			card.type = 0;
		};
	});
	CardLove.Card.renderCards();
	return {
		'sys' : sysrole,
		'player' : player
	}
};

CardLove.Card.checkCard = function(card){
	return (CardLove.Card._cache.sysCurrentShowCard == card.id) ;
}

/**
 * 进行卡片的页面渲染，把img标签添加到页面上
 * 
 */
CardLove.Card.renderCards = function(){
	var doit=function(tag,cardList,containerList){
		var i =0;
		jQuery.each(cardList,function(index,card){
			card.drawImg(tag,containerList[i++]);
			card.areaIndex = i;
		});
	}
	doit(CardLove.Config.imgList.node[0].tag, CardLove.Card._cache.playCards ,CardLove.Card._cache.playContainers);//渲染玩家图片
	doit(CardLove.Config.imgList.node[1].tag, CardLove.Card._cache.sysCards ,CardLove.Card._cache.sysContainers);//渲染AI图片
	return true;
};

/**
 * 系统出牌
 */
CardLove.Card.sysShowCard = function(){
	var showCardIndex =CardLove.Card._cache.sysCurrentShowCard = CardLove.Config.card.sysShowCardArray.pop();
	var showCard = CardLove.Card._cache.cards[showCardIndex];
	var showNode = document.getElementById(CardLove.Config.imgList.node.show.container);
	if(showNode && showCard){
		showNode.innerHtTML = '';
		showCard.drawImg(CardLove.Config.imgList.node.show.tag,document.getElementById(CardLove.Config.imgList.node.show.container));
	}
};

/**
 * 角色选中一张牌子后的入口判断
 */
CardLove.Card.selectCard = function(card){
	
};
/**
 * 得到卡片的相对坐标
 * @param {Number} index
 * @param {Number} 是否为玩家的flag
 */
CardLove.Card.getPosition = function(index,flag){
	var coordinate = CardLove.Config.imgList.Coordinate[flag];
	var start = coordinate.start;
	return [(start[0]+(index-1) * CardLove.Config.imgList.Coordinate.warp),start[1]]
	
};

CardLove.Card.getCardByIndex = function(index,isPlayer){
	var _cache=CardLove.Card._cache;
	var cardList = isPlayer ? _cache.playCards : _cache.sysCards;
	var targetCard = null;
	jQuery.each(cardList,function(i,card){
		if(card.areaIndex == index){
			targetCard = card;
			return;
		}
	});
	return targetCard;
};
/**
 * 进行card交换
 * @param {String} id1 源卡片容器Index
 * @param {String} id2 目标片容器Index
 * @param {String} mode 模式
 * @param {Boolean} targetIsPlayer 是否是玩家控制目标
 * @param {Function} callback 回调函数
 * @return {Boolean}
 */
CardLove.Card.exchange = function(id1,id2,mode,targetIsPlayer,callback){
	var _cache =  CardLove.Card._cache;
	if(_cache.status.isMove){
		_cache.queue.push({
			id1 : id1,
			id2 : id2,
			mode : mode,
			isPlayer : isPlayer,
			callback : callback
		})
		return false;
	}
	_cache.status.isMove = true;
	var nodeObj = CardLove.Config.imgList.node;
	var flag = targetIsPlayer ? '0' : '1';
	var cardList = targetIsPlayer ? _cache.playContainers : _cache.sysContainers;
	
	if(mode == 'send'){
		var card = _cache.cards[id1];
		var isScoring = (id2 == 'scoring');
		var targetPos = CardLove.Card.getPosition(id1,flag);
		var img = jQuery(cardList[id1]).find('img').get('0');
		CardLove.Card.gotoPosition(img,targetPos,isScoring,callback);
		document.getElementById('scoring').appendChild(img);
	}else if(mode == 'exchagnge') {
		var startPos = CardLove.Card.getPosition(id1,flag);
		var targetPos = CardLove.Card.getPosition(id2,flag);
		var startCard =  CardLove.Card.getCardByIndex(id1,targetIsPlayer);
		var targetCart =  CardLove.Card.getCardByIndex(id2,targetIsPlayer);
		CardLove.Card.gotoPosition(jQuery(cardList[id1]).find('img').get(0),startPos,targetPos,false);
		CardLove.Card.gotoPosition(jQuery(cardList[id2]).find('img').get(0),targetPos,startPos,false,callback);
	}
	return true;
};
/**
 * 使用动画到目标地点
 * 
 * @param {String} targetId 需要进行
 * @param {Array} targetPos 目标坐标
 * @param {Function} callback 回调函数
 */
CardLove.Card.gotoPosition = function(targetId,startPos,targetPos,needRotate,callback){
	var effect = new CardLove.Effect(targetId,function(){
		//进行队列操作
		CardLove.Card._cache.status.isMove = false;
		callback && callback();
		var queueObj = CardLove.Card._cache.queue.shift();
		queueObj && CardLove.Card.exchange(queueObj.id1,queueObj.id2,queueObj.mode,queueObj.isPlayer,queueObj.callback);
	});
	var pos = [targetPos[0]-startPos[0],targetPos[1]-startPos[1]];
	effect.translate(pos[0],pos[1]).duration(500);
	if(needRotate){
		effect.rotate(90).start();
	}else{
		effect.start();
	}
};

